#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+5,mod=1e9+7;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
int n,l[N],r[N],f[N],ls[N],rs[N],fa[N],ans[N];
void add(int &x,int y){if((x+=y)>=mod) x-=mod;}
void find(int x,int dep){
	add(ans[x],dep);
	if(ls[x]) find(ls[x],dep+1);
	if(rs[x]) find(rs[x],dep+1);
}
void Splay(int x){
	int P=fa[x];
	if(x==ls[P]){
		int Xrs=rs[x],FP=fa[P];
		rs[x]=P;
		fa[x]=FP;
		ls[P]=Xrs;
		fa[P]=x;
	}
	else{
		int Xls=ls[x],FP=fa[P];
		ls[x]=P;
		fa[x]=FP;
		rs[P]=Xls;
		fa[P]=x;		
	}
	return;
}
signed main(){
	freopen("dream.in","r",stdin);
	freopen("dream.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++){
		l[i]=read();
		f[l[i]]=i;
		r[i]=read();
		f[r[i]]=i;
	}
	for(int i=1;i<=n;i++){
		int x=i,root=1;
		for(int j=1;j<=n;j++) ls[j]=l[j],rs[j]=r[j],fa[j]=f[j];
		while(1){
			int P=fa[x];
			if(x==root){
				break;
			}
			else if(P==root){
				Splay(x);
				break;
			}
			else if((x==ls[P]&&P==ls[fa[P]])||(x==rs[P]&&P==rs[fa[P]])){
				if(fa[P]==root) root=P;
				Splay(P);
				if(fa[x]==root) root=x;
				Splay(x);			
			}
			else{
				if(fa[x]==root) root=x;
				Splay(x);
				if(fa[x]==root) root=x;
				Splay(x);
			} 
		}
		find(i,0);
	}
	for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
	return 0;
}
